package threadpoolexecutor;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : ThreadPoolExecutorDemo
 * @Author : 54061
 * @Date: 2022/2/16 19:54
 */
/*
new ThreadPoolExecutor.AbortPolicy();//拒绝策略, 并报错
    new ThreadPoolExecutor.DiscardPolicy();//拒绝策略, 不报错
    new ThreadPoolExecutor.DiscardOldestPolicy();//拒绝策略, 把等待最久的人添加到阻塞队列中 移除队列中的某个任务
    new ThreadPoolExecutor.CallerRunsPolicy();//拒绝策略, 叫其他的线程对象帮忙完成任务
 */
public class ThreadPoolExecutorDemo {
    public static void main(String[] args) {
        //threadPool 线程池 一共能处理多少个线程 : 总线程数+等待队列的容量
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2,
                3,
                20,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(1),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0; i < 5; i++) {
            final int y = i;
            threadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"World"+y);
                }
            });
        }
        //关闭线程
        threadPool.shutdown();
    }
}
